//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ZeroSpreadedTermStructure.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Yield/ZeroYieldStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::IQuote^ spread, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dc) : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
    CYieldTermStructure^ _Cprm1;
    CQuote^ _Cspread;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phZeroSpreadedTermStructure = NULL;
#endif
        _Cprm1 = safe_cast<CYieldTermStructure^> (prm1);
        _Cprm1->Lock();
        Handle<QuantLib::YieldTermStructure>& _prm1 = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cprm1->GetHandle ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        QuantLib::Compounding _comp = 
            (Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>::IsSome::get (comp) ? (QuantLib::Compounding)comp->Value : QuantLib::Compounding::Continuous); //10
        QuantLib::Frequency _freq = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (freq) ? (QuantLib::Frequency)freq->Value : QuantLib::Frequency::NoFrequency); //10
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dc))
        {
            _Cdc = safe_cast<CDayCounter^> (dc->Value);
            _Cdc->Lock();
        }
        QuantLib::DayCounter& _dc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dc) ? static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()) : QuantLib::DayCounter()); //1
        _ppZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure> (new QuantLib::ZeroSpreadedTermStructure ( _prm1,  _spread,  _comp,  _freq,  _dc ));
        SetZeroYieldStructure (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure>& childNative, Object^ owner) : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phZeroSpreadedTermStructure = NULL;
#endif
	_ppZeroSpreadedTermStructure = &childNative;
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
}
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (QuantLib::ZeroSpreadedTermStructure& childNative, Object^ owner) : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phZeroSpreadedTermStructure = NULL;
#endif
	_ppZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure> (&childNative);
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
    _ZeroSpreadedTermStructureOwner = owner;
    _ZeroYieldStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (CZeroSpreadedTermStructure^ copy) : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phZeroSpreadedTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure> (copy->GetShared());
        _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
    }
}
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (System::Type^ t) : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phZeroSpreadedTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CZeroSpreadedTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (QuantLib::Handle<QuantLib::ZeroSpreadedTermStructure>& childNative, Object^ owner)  : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
	_phZeroSpreadedTermStructure = &childNative;
	_ppZeroSpreadedTermStructure = &static_cast<boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
    _ZeroSpreadedTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (QuantLib::Handle<QuantLib::ZeroSpreadedTermStructure> childNative)  : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
	_phZeroSpreadedTermStructure = &childNative;
	_ppZeroSpreadedTermStructure = &static_cast<boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::CZeroSpreadedTermStructure (QuantLib::ZeroSpreadedTermStructure childNative)  : CZeroYieldStructure(CZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phZeroSpreadedTermStructure = NULL;
#endif
	_ppZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure> (new QuantLib::ZeroSpreadedTermStructure (childNative));
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppZeroSpreadedTermStructure));
}
#endif

Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::~CZeroSpreadedTermStructure ()
{
    if (_ppZeroSpreadedTermStructure != NULL)
    {
	    delete _ppZeroSpreadedTermStructure;
        _ppZeroSpreadedTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::!CZeroSpreadedTermStructure ()
{
    if (_ppZeroSpreadedTermStructure != NULL)
    {
	    delete _ppZeroSpreadedTermStructure;
    }
}
QuantLib::ZeroSpreadedTermStructure& Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::GetReference ()
{
    if (_ppZeroSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppZeroSpreadedTermStructure;
}
boost::shared_ptr<QuantLib::ZeroSpreadedTermStructure>& Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::GetShared ()
{
    if (_ppZeroSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppZeroSpreadedTermStructure;
}
QuantLib::ZeroSpreadedTermStructure* Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::GetPointer ()
{
    if (_ppZeroSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppZeroSpreadedTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ZeroSpreadedTermStructure>& Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::GetHandle ()
{
	if (_phZeroSpreadedTermStructure == NULL)
	{
		_phZeroSpreadedTermStructure = new Handle<QuantLib::ZeroSpreadedTermStructure> (*_ppZeroSpreadedTermStructure);
	}
	return *_phZeroSpreadedTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::HasNative () 
{
	return (_ppZeroSpreadedTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppZeroSpreadedTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppZeroSpreadedTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppZeroSpreadedTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppZeroSpreadedTermStructure)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppZeroSpreadedTermStructure)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppZeroSpreadedTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IZeroSpreadedTermStructure^ Cephei::QL::Termstructures::Yield::CZeroSpreadedTermStructure_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::IQuote^ spread, Microsoft::FSharp::Core::FSharpOption<QL::CompoundingEnum>^ comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dc)
{
    return gcnew CZeroSpreadedTermStructure ( prm1,  spread,  comp,  freq,  dc);
}
